package com.pp.controller;


import com.pp.bean.GovGoodsInfo;
import com.pp.service.GovGoodsInfoService;
import com.pp.util.Respons.ResponseUtil;
import com.pp.util.upload.UploadFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2019-04-02
 */
@Controller
@RequestMapping("/govGoodsInfo")
@Api(value = "商品controller", tags = {"商品类"})
public class GovGoodsInfoController {
    @Autowired
    GovGoodsInfoService goodsInfoService;

    /**
     * 查询商品
     */
     @RequestMapping(value = "/findGood", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "查询商品")
    public List<GovGoodsInfo> findGood(@RequestBody GovGoodsInfo goodsInfo){

    return goodsInfoService.findGood(goodsInfo);
    }

    /**
     * 商品主图上传
     *
     * @return
     */
    @RequestMapping(value = "/saveGoodIndexImage", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "商品主图上传")
    public Map<String, Object> saveGood(@RequestParam("file") MultipartFile file) {
        try {
            return ResponseUtil.getSuccessMap(UploadFile.fileUpload(file),"上传成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return ResponseUtil.getExceptionMap();
        }
    }


    /**
     * 新增商品
     *
     * @return
     */
    @RequestMapping(value = "/saveGood", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增商品")
    public Map<String, Object> saveGood(@RequestBody GovGoodsInfo govGoodsInfo) {
        return goodsInfoService.saveGood(govGoodsInfo);
    }
}
